<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 9/13/12
 * Time: 2:23 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Products_Sizes extends Page {
    /**
     * @var Db_Products_Sizes
     */
    protected $tableObject;

    function __construct() {

        $this->idTableObject = get('id', 'int');
        $this->classTableObject = 'Db_Products_Sizes';
        $this->actionsTableObject = array(
            'addEditAction',
            'saveAction'
		);
        parent::__construct();
    }

    public function defaultAction() {
		if (perm('PRODUCTS_SIZES_EDIT')) {
        	$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
		}
        $allSizes =  Db_Products_Sizes::getAllSizes();

        self::$TPL->assign('allSizes', $allSizes);
        self::$TPL->setBodyTemplate('list_sizes');

        return array(
            'type' => 'display'
        );
    }

    public function addEditAction(){
		perm('PRODUCTS_SIZES_EDIT', self::$PAGE['p_url']);
        if(empty($this->tableObject->objectArray->id)) {
            $this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
        } else {
            self::$TPL->assign('sizes',  $this->tableObject->objectArray);
            $this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' &quot;' . $this->tableObject->objectArray->s_name . '&quot;' , '?action=addEdit', true);
        }
        $orientation = $this->tableObject->getEnumValues('s_orientation');
        self::$TPL->assign('orientation', $orientation);
        self::$TPL->setBodyTemplate('add_edit_sizes');
        return array(
            'type' => 'display'
        );
    }

    public function saveAction() {
		perm('PRODUCTS_SIZES_EDIT', self::$PAGE['p_url']);
        $this->message = t(T_FORM_MSG, 'success');
        $sizesName = strtolower(post('s_name', 'str'));
        $sizesWidth = post('s_width', 'int');
        $sizesHeight = post('s_height', 'int');
        $sizesFaceNumber = post('s_face_number', 'int');
        $sizesOrientation = strtolower(post('s_orientation', 'string'));
        if(!empty($sizesName) && !empty($sizesWidth) && !empty($sizesHeight) && !empty($sizesFaceNumber)) {
            $this->tableObject->s_name = $sizesName;
            $this->tableObject->s_width = $sizesWidth;
            $this->tableObject->s_height = $sizesHeight;
            $this->tableObject->s_face_number = ($sizesFaceNumber < 0 ? 1 : $sizesFaceNumber);
            $this->tableObject->s_orientation = $sizesOrientation;
            if(!$this->tableObject->save()) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        } else {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }
        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

    public function deleteAction() {
		perm('PRODUCTS_SIZES_EDIT', self::$PAGE['p_url']);
        if($this->idTableObject > 0) {
            if(!Db_Products_Sizes::abstractDelete($this->idTableObject)) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        } else {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }
        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

}
